<?php include_once ('function/check_login_function.php'); onlyValid(); onlyPatient();?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Health Tracker</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Health Tracker">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <link rel="shortcut icon" href="ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png">
  </head>

  <body>


<?php include_once('nav.php'); ?>
      
    <div class="container">
    <div class="row">
    <div class="span8">
      <h1>Log</h1>
					<?php
	  					if (isset($_SESSION['message'])) {
						foreach ($_SESSION['message'] as $message) {
							echo '<div class="alert">'.constant($message) . '</div>';
						}
						unset($_SESSION['message']);
					} 
					?>
							
				<?php if ($user->getRole() === 'patient') {?>
					<form action="function/form_function.php" method="POST">
                    Participate in Benchmarking
					<input id="benchmark" name="benchmark" value="benchmark" type="checkbox" <?php if ($user->getBenchmark() === 1) {echo 'checked="checked"';} ?>/> 
					<?php if (isset($_GET['log']) && isset($_GET['mode'])) {
						echo '<input type="hidden" id="from" name="from" value="log.php?benchmark=' . $_GET['log'] . '&mode="' . $_GET['mode'] . '/>';
					} else {
						echo '<input type="hidden" id="from" name="from" value="log.php"/>';
					} 
					?>	
					<input type="submit" name="enterBenchmark" value="Enter" class="btn btn-primary"/> <br /> 
					</form>
					
					<?php include_once('log_form.php'); ?>

					<form action="log.php" method="GET">
					<select id="log" name="log">
					<?php 
						$datalogtype_list = DataLog::getDataLogTypeList();
						foreach ($datalogtype_list as $datalogtype) {
							echo '<option value="'.$datalogtype['datalog_type_id'].'">'.ucwords($datalogtype['datalog_type_name']).'</option>'; 
						}
					?>
					</select>
					<input type="submit" name="mode" value="Visualize" class="btn btn-primary"/> 
					or Benchmark via
					<input type="submit" name="mode" value="Table" class="btn btn-primary"/> 
					<input type="submit" name="mode" value="Graph" class="btn btn-primary"/> <br />
					Note: Benchmarking assumes everyone input the same unit of data.
					<br /> 
					</form>

		
			<?php if (isset($_GET['log']) && isset($_GET['mode']) && ucwords($_GET['mode']) === "Visualize") {
					$datalog_list = $user->getDataLogListForTypePerDay($_GET['log']);
					if ($datalog_list !== null) {
						$first_time = strtotime($datalog_list[0]['datalog_date']);
						$last_time = strtotime($datalog_list[count($datalog_list)-1]['datalog_date']);
						for ($i = $first_time; $i <= $last_time; $i += 86400) {
							$datalog_sum = null;
							foreach ($datalog_list as $datalog) {
								$datalog_time = strtotime($datalog['datalog_date']);
								if ($datalog_time == $i) {
									$datalog_sum = $datalog['datalog_sum'];
									break;
								}
							}
							$data[] = array(date("ymd", $i), $datalog_sum);
						}

					
						$graph = new PHPlot(GRAPH_WIDTH_LARGE, GRAPH_HEIGHT_LARGE); 
						
						 //Specify this random file to be the output file of the image
						$temp_png = GRAPH_DIR . "graph_log_" . $user->getUserID() . ".png";
						$graph->SetOutputfile($temp_png);
						$graph->SetFileFormat(GRAPH_FILEFORMAT); // is default anyway 
						
						//Don't draw the image yet
						$graph->SetPrintImage(0);
						//Don't output the file headers - we're inline!
						$graph->SetIsInline("1"); 
						$graph->SetDataType(GRAPH_DATATYPE);
						$graph->SetDataValues($data);

						//$graph->SetDefaultTTFont("graph/arial.ttf");
						//Specify plotting area details
						$graph->SetPlotType(GRAPH_PLOTTYPE);
						//$graph->SetPlotAreaWorld(0, 0, null, 1000);
						$graph->SetYTickIncrement(GRAPH_YTICK_INCREMENT);
						$graph->SetXTickIncrement(GRAPH_XTICK_INCREMENT);
						
						$graph->SetTTFPath(GRAPH_TTFPATH);				
						$graph->SetDefaultTTFont(GRAPH_TTFFONT);

						$graph->SetFontTTF('generic', GRAPH_TTFFONT, GRAPH_TTFFONT_GENERIC);
						$graph->SetFontTTF('legend', GRAPH_TTFFONT, GRAPH_TTFFONT_LEGEND);
						$graph->SetFontTTF('title', GRAPH_TTFFONT, GRAPH_TTFFONT_TITLE);						
						$graph->SetFontTTF('x_label', GRAPH_TTFFONT, GRAPH_TTFFONT_XLABEL);
						$graph->SetFontTTF('y_label', GRAPH_TTFFONT, GRAPH_TTFFONT_YLABEL);
						$graph->SetFontTTF('x_title', GRAPH_TTFFONT, GRAPH_TTFFONT_XTITLE);
						$graph->SetFontTTF('y_title', GRAPH_TTFFONT, GRAPH_TTFFONT_YTITLE);							
						
						$graph->SetPlotBgColor(GRAPH_PLOT_BGCOLOR);
						$graph->SetBackgroundColor(GRAPH_BGCOLOR); 
						$graph->SetPlotBorderType(GRAPH_PLOT_BORDERTYPE);

						$graph->SetTitle('Time over ' . DataLog::findTypeName($_GET['log']));
						
						//Define the X axis
						$graph->SetXLabel( "Days");
						$graph->SetXDataLabelPos('plotdown');
						  
						//Define the Y axis
						$graph->SetYLabel( DataLog::findTypeName($_GET['log']));
						$graph->SetDataColors(unserialize(GRAPH_DATA_COLORS)); 
						$graph->DrawGraph();
						$graph->PrintImage();
						
						$graph_width=GRAPH_WIDTH;
						$graph_height=GRAPH_HEIGHT;
						echo "<p align=\"center\"><img src=\"{$temp_png}\" width=\"{$graph_width}\" height=\"{$graph_height}\"> </p>"; 
						
						} else {
							echo constant("MSG_GRAPH_UNAVAILABLE") . "<br/><br/>";;
						}					  
					?>
					
					<?php } else if (isset($_GET['log']) && isset($_GET['mode']) && $_GET['mode'] === "Graph") {
						$benchmark_list = Datalog::benchmark($user->getUserID(), $_GET['log']);
						if ($benchmark_list !== null) {
							$first_time = strtotime($benchmark_list[0]['benchmark_date']);
							$last_time = strtotime($benchmark_list[count($benchmark_list)-1]['benchmark_date']);
							for ($i = $first_time; $i <= $last_time; $i += 86400) {
								$datalog_value = null;
								$benchmark_value = null;
								foreach ($benchmark_list as $benchmark) {
									$benchmark_time = strtotime($benchmark['benchmark_date']);
									if ($benchmark_time == $i) {
										$datalog_value = $benchmark['datalog_average'];
										$benchmark_value = $benchmark['benchmark_value'];
										break;
									}
								}
								$data[] = array(date("ymd", $i), $datalog_value, $benchmark_value);
							}						
							$graph = new PHPlot(GRAPH_WIDTH_LARGE, GRAPH_HEIGHT_LARGE); 
							
							 //Specify this random file to be the output file of the image
							$temp_png = GRAPH_DIR . "graph_benchmark_" . $user->getUserID() . ".png";
							$graph->SetOutputfile($temp_png);
							$graph->SetFileFormat(GRAPH_FILEFORMAT); // is default anyway 
							
							//Don't draw the image yet
							$graph->SetPrintImage(0);
							//Don't output the file headers - we're inline!
							$graph->SetIsInline("1"); 
							$graph->SetDataType(GRAPH_DATATYPE);
							$graph->SetDataValues($data);

							//$graph->SetDefaultTTFont("graph/arial.ttf");
							//Specify plotting area details
							$graph->SetPlotType(GRAPH_PLOTTYPE);
							$graph->SetYTickIncrement(GRAPH_YTICK_INCREMENT);
							$graph->SetXTickIncrement(GRAPH_XTICK_INCREMENT);
							
							$graph->SetTTFPath(GRAPH_TTFPATH);				
							$graph->SetDefaultTTFont(GRAPH_TTFFONT);

							$graph->SetFontTTF('generic', GRAPH_TTFFONT, GRAPH_TTFFONT_GENERIC);
							$graph->SetFontTTF('legend', GRAPH_TTFFONT, GRAPH_TTFFONT_LEGEND);
							$graph->SetFontTTF('title', GRAPH_TTFFONT, GRAPH_TTFFONT_TITLE);						
							$graph->SetFontTTF('x_label', GRAPH_TTFFONT, GRAPH_TTFFONT_XLABEL);
							$graph->SetFontTTF('y_label', GRAPH_TTFFONT, GRAPH_TTFFONT_YLABEL);
							$graph->SetFontTTF('x_title', GRAPH_TTFFONT, GRAPH_TTFFONT_XTITLE);
							$graph->SetFontTTF('y_title', GRAPH_TTFFONT, GRAPH_TTFFONT_YTITLE);	
	
							$graph->SetPlotBgColor(GRAPH_PLOT_BGCOLOR);
							$graph->SetBackgroundColor(GRAPH_BGCOLOR); 
							$graph->SetPlotBorderType(GRAPH_PLOT_BORDERTYPE);
							 
							$graph->SetTitle('Benchmark for ' . DataLog::findTypeName($_GET['log']));
							
							//Define the X axis
							$graph->SetXLabel( "Days");
							$graph->SetXDataLabelPos('plotdown');
							  
							//Define the Y axis
							$graph->SetYLabel( DataLog::findTypeName($_GET['log']));
							$graph->SetDataColors(unserialize(GRAPH_DATA_COLORS)); 
							$graph->setLegend( array( "Your data", "Average benchmark value" ));
							$graph->DrawGraph();
							$graph->PrintImage();
							
							$graph_width=GRAPH_WIDTH;
							$graph_height=GRAPH_HEIGHT;
							echo "<p align=\"center\"><img src=\"{$temp_png}\" width=\"{$graph_width}\" height=\"{$graph_height}\"> </p>"; 
						} else {
							echo constant("MSG_GRAPH_UNAVAILABLE") . "<br/><br/>";;
						}					  
					?>
					<?php } else if (isset($_GET['log']) && isset($_GET['mode']) && $_GET['mode'] === "Table") {?>
						<?php $benchmark_list = Datalog::benchmark($user->getUserID(), $_GET['log']); ?>
						<?php if ($benchmark_list !== null) {?>
						<table class="table">
						<tr>
						<th>Patient average</th>
						<th>Benchmark for <?php echo DataLog::findTypeName($_GET['log']) ?></th>
						<th>Date</th>
						</tr>
						<?php 
							
								foreach ($benchmark_list as $benchmark) {
									echo '<tr>';
									echo '<td>' . $benchmark['datalog_average'] . '</td>';
									echo '<td>' . $benchmark['benchmark_value'] . '</td>';
									echo '<td>' . $benchmark['benchmark_date'] . '</td>';
									echo '</tr>';
								}
							} else {
								echo constant("MSG_BENCHMARK_DATA") . "<br/><br/>";
							}
						?>
						</table>					
					<?php } else {?>	
					
					<?php $datalog_list = $user->getDatalogList(); ?>
					<?php if ($datalog_list !== null) {?>
					<table class="table">
					<tr>
					<th>Type</th>
					<th>Value</th>
					<th>Unit</th>
					<th>Notes</th>
					<th>Date</th>
					<th></th>
					</tr>
					<?php 
						
							foreach ($datalog_list as $datalog) {
								echo '<tr>';
								echo '<td>' . DataLog::findTypeName($datalog['datalog_type_id']) . '</td>';
								echo '<td>' . $datalog['datalog_value'] . '</td>';
								echo '<td>' . $datalog['datalog_unit'] . '</td>';
								echo '<td>' . $datalog['datalog_note'] . '</td>';
								echo '<td>' . $datalog['created_at'] . '</td>';
								echo '<td>';
									echo '<form action="function/form_function.php" method="POST">';
									echo '<input type="hidden" id="datalogID" name="datalogID" value="' . $datalog['datalog_id'] . '"/>';
									if (isset($_GET['log']) && isset($_GET['mode'])) {
										echo '<input type="hidden" id="from" name="from" value="log.php?benchmark=' . $_GET['log'] . '&mode="' . $_GET['mode'] . '/>';
									} else {
										echo '<input type="hidden" id="from" name="from" value="log.php"/>';
									} 
									echo '<input type="submit" name="removeLog" value="Remove Log" class="btn btn-primary"/> ';
									echo' </form>';
								echo '</td>';
								echo '</tr>';
							}
						}
					?>
					</table>				
					
					<?php } ?>
				<?php } ?>
				
    </div>
    </div>
    </div> <!-- /container -->
  </body>
</html>